package com.example.statistics.ui.main

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.statistics.R
import com.example.statistics.ex.glide
import com.example.statistics.ex.slimAdapter
import com.example.statistics.ui.LoginActivity
import com.example.statistics.ui.SalesSlipActivity
import com.example.statistics.ui.SplashActivity
import com.example.statistics.ui.bank.*
import com.example.statistics.ui.customer.CustomerListActivity
import com.example.statistics.ui.inventory.InventoryStockListActivity
import com.example.statistics.ui.inventory.InventorySubListActivity
import com.example.statistics.ui.market.*
import com.example.statistics.ui.port.EstimateActivity
import com.example.statistics.ui.port.ExchangeRateListActivity
import com.example.statistics.ui.port.StorageListActivity
import com.example.statistics.ui.print.PrintActivity
import com.example.statistics.ui.product.ProductInfoListActivity
import com.example.statistics.ui.product.ProductTreeActivity
import com.example.statistics.ui.purchase.PurchaseAgentListActivity
import com.example.statistics.ui.sales.MarketSalesDailyListActivity
import com.example.statistics.ui.sales.MarketSalesListActivity
import com.example.statistics.ui.sales.SalesStatisticsListActivity
import com.example.statistics.ui.shipment.ShipmentListActivity
import com.example.statistics.ui.ysy.ProductListActivity
import com.example.statistics.ui.ysy.StorageSplitActivity
import com.example.statistics.ui.yw.MaterialListActivity
import com.example.statistics.ui.yw.PreProductProfitListActivity
import com.example.statistics.ui.yw.ProductSalesListActivity
import com.example.statistics.ui.yw.ProductTypeListActivity
import com.example.statistics.utils.activityForResult
import kotlinx.android.synthetic.main.fragment_home.view.*

class HomeFragment : Fragment() {
    private val group = listOf(
        FunctionGroup(
            title = "港口",
            listOf(
                Function("汇率", R.drawable.ic_fun_rate, ExchangeRateListActivity::class.java),
                Function("冷库", R.drawable.ic_fun_storage, StorageListActivity::class.java),
                Function("估算", R.drawable.ic_fun_estimate, EstimateActivity::class.java)
            ),
        ),
        FunctionGroup(
            title = "豫双源",
            listOf(
                Function("商品", clazz = ProductListActivity::class.java),
                Function("拆分", clazz = StorageSplitActivity::class.java),
            ),
        ),
        FunctionGroup(
            title = "豫福森",
            listOf(
                Function("库存", clazz = InventoryStockListActivity::class.java),
                Function("销售单", clazz = InventorySubListActivity::class.java),
            ),
        ),
        FunctionGroup(
            title = "银行", listOf(
                Function("现金银行", R.drawable.ic_fun_bank, clazz = CashBankActivity::class.java),
                Function("银行流水", R.drawable.ic_fun_bank_flow, clazz = BankFlowListActivity::class.java),
                Function("一般费用", clazz = GeneralExpenseListActivity::class.java),
                Function("其他收入", clazz = OtherIncomeListActivity::class.java),
                Function("调账", clazz = AdjustmentAddActivity::class.java),
                Function("流水", clazz = SalesSlipActivity::class.java),
            )
        ),
        FunctionGroup(
            title = "依汶", listOf(
                Function("原料", clazz = MaterialListActivity::class.java),
                Function("成品", clazz = ProductSalesListActivity::class.java),
                Function("拆分", clazz = PreProductProfitListActivity::class.java),
                Function("成品", clazz = ProductTypeListActivity::class.java)
            )
        ),
        FunctionGroup(
            title = "物流港", listOf(
                Function(
                    "车",
                    src = R.drawable.ic_fun_car,
                    clazz = com.example.statistics.ui.car.CarListActivity::class.java
                ),
                Function(
                    "流向",
                    src = R.drawable.ic_fun_flow,
                    clazz = MarketSalesListActivity::class.java
                ),
                Function(
                    "日报",
                    src = R.drawable.ic_fun_form,
                    clazz = MarketSalesDailyListActivity::class.java
                ),
                Function(
                    "产品",
                    src = R.drawable.ic_fun_pig,
                    clazz = ProductInfoListActivity::class.java
                ),
                Function("销售", clazz = SalesStatisticsListActivity::class.java)
            )
        ),
        FunctionGroup(
            title = "开拓市场", listOf(
                Function("市场客户", clazz = MarketCustomerListActivity::class.java),
                Function("市场", clazz = MarketInfoListActivity::class.java),
                Function("城市树", clazz = MarketCityTreeActivity::class.java),
                Function("城市", clazz = MarketCityListActivity::class.java)
            )
        ),
        FunctionGroup(
            title = "其他", listOf(
                Function("主页", clazz = SplashActivity::class.java),
                Function("登录", clazz = LoginActivity::class.java),
                Function("客户", clazz = CustomerListActivity::class.java),
                Function("代理", clazz = PurchaseAgentListActivity::class.java),
                Function("商品信息", clazz = ProductInfoListActivity::class.java),
                Function("商品树", clazz = ProductTreeActivity::class.java),
                Function("库存", clazz = InventorySubListActivity::class.java),
                Function("蓝牙打印", clazz = PrintActivity::class.java),
                Function("打印测试", clazz = ShipmentListActivity::class.java)
            )
        )
    )

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        slimAdapter<FunctionGroup>(R.layout.list_item_recycler_view) { functionGroup ->
            this.text(R.id.tv_title, functionGroup.title)
                .with<RecyclerView>(R.id.recycler_view) { recyclerView ->


                    recyclerView.layoutManager = GridLayoutManager(context, 4)
                    slimAdapter<Function>(R.layout.list_item_function) { function ->
                        this.text(R.id.tv_title, function.title)
                            .glide(R.id.image_view, function.src)
                            .clicked {
                                val intent = Intent(activity, function.clazz)
                                activity?.activityForResult(intent) {
                                    val data = it?.getSerializableExtra("data")
                                    val b = data
                                }
                            }
                    }.attachTo(recyclerView).setDataList(functionGroup.list)
                }

        }.attachTo(view.recycler_view).setDataList(group)
    }


    data class Function(
        val title: String,
        val src: Int = R.drawable.ic_pig,
        val clazz: Class<out Activity>
    )

    data class FunctionGroup(val title: String, val list: List<Function>)
}